/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BUS;

import DAO.KhachHangDAO;
import Entity.KhachHang;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author copang
 */
public class KhachHangImpl implements KhachHangBUS{

    private KhachHangDAO dao;

    public KhachHangImpl() {
        dao = new DAO.KhachHangImpl();
    }

    public ArrayList getKhachHang() {
        ResultSet rs = dao.getAll();

        ArrayList list = new ArrayList();
        KhachHang data;

        try {
            while(rs.next()) {
                data = new KhachHang(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getBoolean(4), rs.getString(5),
                        rs.getString(6), rs.getString(7), rs.getInt(8));
                list.add(data);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAO.KhachHangImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

    public boolean add(KhachHang data) {
        if (data.getHoTenKH().isEmpty())
            return false;

        return dao.add(data);
    }

    public boolean delete(int MaKH) {
        return dao.delete(MaKH);
    }

    public boolean edit(KhachHang data) {
        return dao.edit(data);
    }

    public boolean addTienNo(int maKH, int tien) {
        return dao.addTienNo(maKH, tien);
    }

    public KhachHang getByID(int maKH) {
        ResultSet rs = dao.getByID(maKH);
        KhachHang data = null;
        try {
            rs.next();
            data = new KhachHang(
                    rs.getInt(1), 
                    rs.getInt(2), 
                    rs.getString(3), 
                    rs.getBoolean(4), 
                    rs.getString(5), 
                    rs.getString(6), 
                    rs.getString(7), 
                    rs.getInt(8));
            
        } catch (SQLException ex) {
            Logger.getLogger(KhachHangImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return data;
    }

}
